package com.bbcloud.service.impl;

import com.bbcloud.common.Const;
import com.bbcloud.common.ServerResponse;
import com.bbcloud.common.TokenCache;
import com.bbcloud.dao.LinkMapper;
import com.bbcloud.dao.UserMapper;
import com.bbcloud.pojo.Link;
import com.bbcloud.pojo.User;
import com.bbcloud.service.IFileService;
import com.bbcloud.service.ILinkService;
import com.bbcloud.util.IDUtil;
import com.bbcloud.util.PropertiesUtil;
import com.bbcloud.vo.LinkVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * Created by liuzhu on 2018/12/5.
 */
@Service("iLinkService")
public class LinkServiceImpl implements ILinkService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LinkMapper linkMapper;
    @Autowired
    private IFileService iFileService;

    public ServerResponse sendLink(String token, String userId, int linkType, MultipartFile file,String linkImage,String path, String description, int usedTime, int times, int mileage){
        User user = userMapper.selectUser(userId);
        if (user == null){
            return ServerResponse.createByErrorMessage("The user doesn't exist.");
        }
        String tokenValid = TokenCache.getKey(TokenCache.TOKEN_PREFIX+user.getEmail());
        if (!StringUtils.equals(tokenValid,token)){
            return ServerResponse.createByErrorMessage("Please log in again");
        }
        Link link = new Link();
        if (linkType == Const.LinkType.LINKTYPE_NORMAL){
            String targetFileName = iFileService.upload(file,path);
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
            Map fileMap = Maps.newHashMap();
            fileMap.put("uri",targetFileName);
            fileMap.put("url",url);
            link.setLinkImages(url);
        }
        else {
            link.setLinkImages(linkImage);
        }
        link.setLinkId(IDUtil.linkID());
        link.setUserId(userId);
        link.setLinkType(linkType);
        link.setDescription(description);
        link.setMileage(mileage);
        link.setUsedTime(usedTime);
        link.setTimes(times);
        int rowCount = linkMapper.insert(link);
        if (rowCount == 0){
            return ServerResponse.createByErrorMessage("发布link失败");
        }
        return ServerResponse.createBySuccess();

    }

    public ServerResponse<PageInfo> list(String token, String userId, int pageNum, int pageSize, String orderBy){
        User user = userMapper.selectUser(userId);
        if (user == null){
            return ServerResponse.createByErrorMessage("The user doesn't exist.");
        }
        String tokenValid = TokenCache.getKey(TokenCache.TOKEN_PREFIX+user.getEmail());
        if (!StringUtils.equals(tokenValid,token)){
            return ServerResponse.createByErrorMessage("Please log in again");
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Link> linkList = linkMapper.selectAllLink();
        List<LinkVo> linkVoList = Lists.newArrayList();
        for (Link link:linkList){
            LinkVo linkVo = assembleLinkListVo(link);
            linkVoList.add(linkVo);
        }
        PageInfo pageResult = new PageInfo(linkList);
        pageResult.setList(linkVoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    private LinkVo assembleLinkListVo (Link link){
        LinkVo linkVo = new LinkVo();
        linkVo.setUserId(link.getUserId());
        User user = userMapper.selectUser(link.getUserId());
        if (user != null){
            linkVo.setUser(user);
        }
        linkVo.setLinkId(link.getLinkId());
        linkVo.setLinkType(link.getLinkType());
        linkVo.setLinkImages(link.getLinkImages());
        linkVo.setDescription(link.getDescription());
        linkVo.setUsedTime(link.getUsedTime());
        linkVo.setTimes(link.getTimes());
        linkVo.setMileage(link.getMileage());
        linkVo.setZan(link.getZan());
        linkVo.setCreateTime(link.getCreateTime());
        linkVo.setUpdateTime(link.getUpdateTime());
        return  linkVo;
    }
}
